require('es6-promise').polyfill();

import axios from 'axios'

function errorAction(error) {
  return {
    type: 'ERROR_WECHAT_USER',
    error
  }
}

function deleteWechatUserData(id) {
  return {
    type: 'DELETE_WECHAT_USER_DATA',
    id
  };
}

export function apiMsgAction(msg) {
  return {
    type: 'API_MSG',
    msg
  }
}

export function deleteWechatUserDataAsync(data){
  return function(dispatch) {
    return axios.get('/api/wechatUser/delete?id='+data.id).then(
      resp => dispatch(loadWechatUserDataAsync({
        offset: data.offset,
        limit: data.limit,
        username: data.search
      })),
      error => dispatch(apiMsgAction(error.data.errMsg))
    );
  };
}


function loadWechatUserData(data) {
  return {
    type: 'LOAD_WECHAT_USER_DATA',
    data
  }
}

export function loadWechatUserDataAsync(data) {
  return function(dispatch) {
    return axios.post('/api/wechatUser',data).then(
      resp => dispatch(loadWechatUserData(resp.data)),
      error => dispatch(apiMsgAction(error.data.errMsg))
    );
  };
}
